/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rh.gui.impl;

import com.rh.gui.Gui;
import com.rh.gui.RenderEvent;

import com.rh.game.Character;
import com.rh.game.EquipmentChange;
import com.rh.game.EventsAndChat;
import com.rh.game.HasEquipment;
import com.rh.game.GameClothing;
import com.rh.game.GameObject;
import com.rh.game.Location;
import com.rh.game.World;

import com.rh.game.chat.Chat;
import com.rh.game.chat.ChatResponse;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

/**
 *
 * @author Hayden
 */
public class Gui1 implements Gui {
    public boolean render(final List<RenderEvent> events, final World world)  {
        boolean gameOver = false;
        
        System.out.println("** RENDER ** " + world);
        System.out.println("You are located at: " + world.getPlayer().getLocation());
        System.out.println("Exits are: ");
        for (Location loc : world.getPlayer().getLocation().getExits())  {
            System.out.println("  " + loc);
        }
        System.out.println("You can see: ");
        for (GameObject go : world.getGameObjectsAroundPlayer())  {
            System.out.println("  " + go);
        }
        
        for (RenderEvent re : events)  {
            System.out.println("  >>> " + re);
            if (re.getType() == RenderEvent.Type.CHAT)  {
                gameOver = chat2(re.getID(), world);
                if (gameOver)  break;
            }
            else if (re.getType() == RenderEvent.Type.MALE_TO_FEMALE)  {
                JOptionPane.showMessageDialog(null, re.getCharacter().getName() + " HAS BEEN CHANGED INTO A GIRL!", "Male to Female Transformation", JOptionPane.INFORMATION_MESSAGE);
            }
            else if (re.getType() == RenderEvent.Type.FINAL_SWAP)  {
                JOptionPane.showMessageDialog(null, "[Insert impress graphic of Final Swap here]", "Final Swap", JOptionPane.INFORMATION_MESSAGE);
            }
            else if (re.getType() == RenderEvent.Type.GAME_OVER)  {
                gameOver = true;
                break;
            }
        }
        
        return gameOver;
    }
    
    private boolean chat2(final String pChatID, final World world) {
        return ConversationJDialog.chat(this, world, pChatID);
    }
    
    private boolean chatSimple(final String pChatID, final World world)  {
        EventsAndChat ec = world.getChat(pChatID);
        System.out.println(pChatID + " ==> " + ec);

        while (ec != EventsAndChat.NULL)  {
            // Render the Events
            boolean gameOver = this.render(ec.getEvents(), world);
            if (gameOver)  return true;
            
            // Now do the Chat
            Chat c = ec.getChat();
            if (c == Chat.NULL)  break;
            
            String message = c.getText();
            String responses = "";
            for (ChatResponse cr : c.getResponses())  {
                responses = responses + "\n(" + cr.getID() + ") " + cr.getText();
            }

            String resp = JOptionPane.showInputDialog(null, message + responses, "Chat with " + c.getSpeaker().getName(), JOptionPane.PLAIN_MESSAGE);

            if ((resp == null) || (c.getResponses().size() == 0))  {
                break;
            }            

            String nextChatID = null;
            for (ChatResponse cr : c.getResponses())  {
                if (cr.getID().equals(resp))  {
                    nextChatID = cr.getNextChatID();
                }
            }

            if (nextChatID == null)  {
                System.out.println("Repsonse not recognized!");
            }
            else  {
                ec = world.getChat(nextChatID);
            }
        }
        
        return false;
    }
    
    public String getUserInput()  {
        return JOptionPane.showInputDialog(null, "What do you want to do?", "User Input", JOptionPane.PLAIN_MESSAGE);
    }    
    
    public EquipmentChange character(Character whom)  {
        return CharacterJPanel.character(whom);
    }
    
    private EquipmentChange character2(Character whom)  {
        System.out.println("=== CHARACTER ===");
        System.out.println(whom.getName() + ", " + whom.getGender());
        System.out.println("Wearing: ");
        for (GameClothing gc : whom.getWearing())  {
            System.out.println("  " + gc);
        }
        System.out.println("Inventory: " + whom.getInventory());

        HasEquipment1 he = new HasEquipment1();
        he.getInventory().addAll(whom.getInventory());
        he.getWearing().addAll(whom.getWearing());
        
        String wear = JOptionPane.showInputDialog(null, "Wear/Take off what?", "Equip", JOptionPane.PLAIN_MESSAGE);
        if (wear == null)  return EquipmentChange.NULL;
        
        while (wear.length() > 0)  {
            GameClothing putOn = null;
            Character.BodyPart part = null;
            GameClothing disrobed = null;
            
            for (GameObject go : he.getInventory())  {
                if (!(go instanceof GameClothing))  continue;
                putOn = (GameClothing) go;
            }
            
            // Take off anything that is there
            if (putOn != null)  {
                for (GameClothing gc : he.getWearing())  {
                    if (gc.getBodyPart() == putOn.getBodyPart())  {
                        disrobed = gc;
                        break;
                    }
                }
            }
            
            // Disrobe?
            if (disrobed == null)  {
                for (GameClothing gc : he.getWearing())  {
                    if (gc.getID().equals(wear))  {
                        disrobed = gc;
                        break;
                    }
                }                
            }
            
            if (disrobed != null)  {
                he.getWearing().remove(disrobed);
                he.getInventory().add(disrobed);
            }
            if (putOn != null)  {
                he.getWearing().add(putOn);
                he.getInventory().remove(putOn);                
            }
            
            System.out.println(">>" + he);
            wear = JOptionPane.showInputDialog(null, "Wear/Take off what?", "Equip", JOptionPane.PLAIN_MESSAGE);
            if (wear == null)  return EquipmentChange.NULL;
        }
                
        return computeEquipmentChange(whom, he);
    }
    
    public static EquipmentChange computeEquipmentChange(Character whom, HasEquipment he)  {
        List<GameClothing> on = new ArrayList<GameClothing>();
        List<GameClothing> off = new ArrayList<GameClothing>();
        
        on.addAll(he.getWearing());
        on.removeAll(whom.getWearing());
        
        off.addAll(whom.getWearing());
        off.removeAll(he.getWearing());
        
        if (on.size() + off.size() == 0)  return EquipmentChange.NULL;
        return new EquipmentChange(GameObject.EMPTY_LIST, on, off);
    }
}
